package org.example.erpapicommon.Util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.List;

public class ApiPageResult<T> {
    private Integer code;
    private String message;
    @JsonIgnore
    private Pageable<T> page;
    private boolean success;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private List<T> data;
    private int pageSize;
    private int pageNumber;
    private int totalPage;
    private int totalRow;

    public ApiPageResult() {}

    public ApiPageResult(Pageable<T> page) {
        this.page = page;
        this.data = page.getList();
        this.pageSize = page.getPageSize();
        this.pageNumber = page.getPageNumber();
        this.totalPage = page.getTotalPage();
        this.totalRow = page.getTotalRow();
    }

    public static <T> ApiPageResult<T> create(ResponseType responseType, Pageable<T> page) {
        ApiPageResult<T> result = new ApiPageResult<>(page);
        result.setCode(responseType.code());
        result.setMessage(responseType.message());
        result.setSuccess(responseType == ResponseType.OK);
        return result;
    }

    public static <T> ApiPageResult<T> create(ResponseType responseType, Pageable<T> page, String message) {
        ApiPageResult<T> result = new ApiPageResult<>(page);
        result.setCode(responseType.code());
        result.setMessage(message);
        result.setSuccess(responseType == ResponseType.OK);
        return result;
    }

    public Integer getCode() {
        return code;
    }

    public ApiPageResult<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public List<T> getData() {
        return data;
    }

    public ApiPageResult<T> setData(List<T> data) {
        this.data = data;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public ApiPageResult<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public int getPageSize() {
        return pageSize;
    }

    public ApiPageResult<T> setPageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public ApiPageResult<T> setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
        return this;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public ApiPageResult<T> setTotalPage(int totalPage) {
        this.totalPage = totalPage;
        return this;
    }

    public int getTotalRow() {
        return totalRow;
    }

    public ApiPageResult<T> setTotalRow(int totalRow) {
        this.totalRow = totalRow;
        return this;
    }

    public boolean isSuccess() {
        return success;
    }

    public ApiPageResult<T> setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public static <T> ApiPageResult<T> success(Pageable<T> page) {
        return create(ResponseType.OK, page);
    }

    public static <T> ApiPageResult<T> success(Pageable<T> page, String message) {
        return create(ResponseType.OK, page, message);
    }
}
